using System.Threading;
using System.Threading.Tasks;
using MediatR;
using B.S.BasicData.EroorCode;
using B.S.BasicData.Domain;
using B.S.BasicData.Infrature;

public class ProindexinfoDeleteHandler : IRequestHandler<ProindexinfoDeleteRequest, ApiResult<int>>
{
    private readonly IBaseRepository<Proindexinfo> _repository;
    public ProindexinfoDeleteHandler(IBaseRepository<Proindexinfo> repository)
    {
        _repository = repository;
    }

    public async Task<ApiResult<int>> Handle(ProindexinfoDeleteRequest request, CancellationToken cancellationToken)
    {
        var entity = await _repository.GetByIdAsync(request.Id);
        if (entity == null)
            return ApiResult<int>.Fail("未找到要删除的数据");
        if (entity.IsDeleted)
            return ApiResult<int>.Fail("该数据已被删除");
        entity.IsDeleted = true;
        try
        {
            await _repository.UpdateAsync(entity);
            return ApiResult<int>.Success(1, "删除成功");
        }
        catch (System.Exception ex)
        {
            return ApiResult<int>.Fail($"删除失败: {ex.Message}");
        }
    }
} 